//
//  TJOrderRecardListController.m
//  Teleport
//
//  Created by hc-101 on 2020/6/12.
//  Copyright © 2020 xtjun. All rights reserved.
//订单记录

#import "TJOrderRecardListController.h"
#import "TJOrderRecordListModel.h"
#import "TJOrderRecardListCell.h"
#import "TJOrderDetailControntroller.h"
#import "TJOrderListRecordScreeningController.h"

@interface TJOrderRecardListController ()

@property (assign, nonatomic) int pageIndex;
@property (copy, nonatomic) NSString *payType;
/**方向（BUY，SELL） */
@property (copy, nonatomic) NSString *direction;
@property (strong, nonatomic) NSMutableArray *datas;



@end

static NSString * const orderRecardListCell = @"orderRecardListCell";

@implementation TJOrderRecardListController
-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
     [self loadNewData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
  
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = 170;
    self.tableView.backgroundColor = UIColor_Hex(0xf2f2f2);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.navigationBar.rightBarButton = [TJButtonBarButton barButtonWithTitle:@"筛选".localizedString target:self action:@selector(didClickNavBarRightButton)];
    [self.tableView registerClass:TJOrderRecardListCell.self forCellReuseIdentifier:orderRecardListCell];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.bottom.offset(0);
    }];
    
   
 
    [self setUpData];
}

- (void)didClickNavBarRightButton {
    TJOrderListRecordScreeningController *orderListRecordScreeningController = [[TJOrderListRecordScreeningController alloc] init];
    [orderListRecordScreeningController setSelectComplete:^(NSArray<TJOrderListScreeningModel *> * _Nonnull models) {
        //订单类型
        TJOrderListScreeningModel *typeModel = models.firstObject;
        //支付方式
        TJOrderListScreeningModel *payTypeModel = models.lastObject;

        
        self.direction = typeModel.identifier;
        self.payType = payTypeModel.identifier;
    
        NSLog(@"direction = %@,payType = %@",self.direction,  self.payType);
        [self loadNewData];
    }];
    [self presentViewController:orderListRecordScreeningController animated:NO completion:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJOrderRecardListCell *cell = [tableView dequeueReusableCellWithIdentifier:orderRecardListCell forIndexPath:indexPath];
    cell.model = self.datas[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TJOrderRecordListModel *model = self.datas[indexPath.row];
    TJOrderDetailControntroller *orderDetailControntroller = [[TJOrderDetailControntroller alloc] init];
    orderDetailControntroller.orderId = model.id;
    [self.navigationController pushViewController:orderDetailControntroller animated:YES];
}
- (void)setUpData{
   

    self.tableView.mj_header = self.refreshHeader;
    self.tableView.mj_footer = self.refreshFooter;

}
- (void)loadNewData{
    _pageIndex = 1;
  
    NSDictionary *parameters = @{
        
        @"userId": TJUserInfo.readInfo.userId,
        @"size": @(10),
        @"payType": self.payType ?: @"",//支付方式 不传为全部订单收款方式,多种的话用-拼接(ALIAY,WECHAT,BANKCARD) 如 ALIAY-WECHAT
        @"page": @(_pageIndex),
        @"direction":self.direction ?: @"",
    };
    
    weakify(self);
    [TJNetworking.shareNetworking POSTNew:@"otc/order/ownOrderListNew" parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSDictionary *data) {
        
        weakself.displayEmpty = YES;
        
        [weakself.datas removeAllObjects];
        weakself.datas = [TJOrderRecordListModel mj_objectArrayWithKeyValuesArray:data[@"data"]];
        [weakself footerViewIsHidden:weakself.datas];
        [weakself.tableView reloadData];
        [weakself.tableView.mj_header endRefreshing];
    } failure:^(NSError * _Nonnull error) {
        [weakself.tableView reloadData];
        [weakself.tableView.mj_header endRefreshing];
    }];
    
}

- (void)loadMoreData {
    _pageIndex++;
    
   NSDictionary *parameters = @{
          
          @"userId": TJUserInfo.readInfo.userId,
          @"size": @(10),
          @"payType": self.payType ?: @"",//支付方式 不传为全部订单收款方式,多种的话用-拼接(ALIAY,WECHAT,BANKCARD) 如 ALIAY-WECHAT
          @"page": @(_pageIndex),
          @"direction":self.direction ?: @"",
      };
    
    weakify(self);
    [TJNetworking.shareNetworking POSTNew:@"otc/order/ownOrderListNew" parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSDictionary *data) {
        
        NSArray *arr = [TJOrderRecordListModel mj_objectArrayWithKeyValuesArray:data[@"data"]];
        
        [weakself.datas addObjectsFromArray:arr];
         [weakself footerViewIsHidden:arr];
        [weakself.tableView reloadData];
        [weakself.tableView.mj_footer endRefreshing];
        
        
    } failure:^(NSError * _Nonnull error) {
        [weakself.tableView.mj_footer endRefreshing];
    }];
}

- (void)footerViewIsHidden:(NSArray *)arr {
    if (arr.count < 10) {
        self.tableView.mj_footer.hidden = YES;
    }else{
        self.tableView.mj_footer.hidden = NO;
    }
}
@end
